﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;

namespace PanoramaTestApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        public static string _uriStringLocal = "http://192.168.0.102";
        public static string _uriStringInternet = "http://quantology.dyndns.org";
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            
        }

        public static void CheckOnlineStatus(string currentUri)
        {
            
            var client = new WebClient();
            client.OpenReadCompleted += (sender, args) =>
                                            {
                                                try
                                                {
                                                    if (args.Error != null)
                                                    {
                                                        MessageBox.Show("Server not online");
                                                        
                                                        
                                                    }
                                                    else
                                                    {
                                                        MessageBox.Show("Server online");
                                                        
                                                    }
                                                }
                                                finally
                                                {

                                                }
                                            };

            
            client.OpenReadAsync(new Uri(currentUri));
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                //App.ViewModel.LoadData();
            }
        }

        private void RefreshExecuted(object sender, System.EventArgs e)
        {
            App.ViewModel.LoadData();
        }

        private void DeleteExecuted(object sender, System.EventArgs e)
        {
            App.ViewModel.DeleteSelectedData();
        }

        private void MoveExecuted(object sender, System.EventArgs e)
        {
            App.ViewModel.MoveSelectedData();
        }

        private void SettingsExecuted(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/SettingsPage.xaml", UriKind.Relative));
        }
    }
}